﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZMotion;

namespace IOModule
{
    public enum StandInputName
    {
        //Auto = 0,
        Start=0,
        Stop,
        Reset,
        EStop,
        Door
        //Door1,
        //Door2,
        //Door3,
        //LightSenser,
        //LinghtSenser1
        
    }
    public enum StandOutPutName
    {
        TowerRed = 0,
        TowerGreen,
        TowerYellow,
        ButtonRed,
        ButtonGreen,
        ButtonYellow,
        Bizz
    }
    static public class IOManager
    {
        static public IOSetting ioSetting;

        static public bool bOpenedSettingForm;
        static public FormIOSetting settingForm;
        //static public Dictionary<string, TableActioner> tableActioners;
        static public void Init()
        {
            ioSetting = IOSetting.Load();
            foreach (IOInputItem item in ioSetting.listInputSettings)
            {
                item.RefreshActioner();
            }
            foreach (IOOutputItem item in ioSetting.listOutputSettings)
            {
                item.RefreshActioner();
            }
            foreach (IOInputItem item in ioSetting.listStandInputs)
            {
                item.RefreshActioner();
            }
            foreach (IOOutputItem item in ioSetting.listStandOutputs)
            {
                item.RefreshActioner();
            }
            //System.Threading.Thread thread = new System.Threading.Thread(InitActionner);
            //thread.IsBackground = true;
            //thread.Start();
            FrameManager.FrameManager.saveProject += FrameManager_saveProject;
            FrameManager.FrameManager.saveAsProject += FrameManager_saveAsProject;
            
        }
       

        private static bool FrameManager_saveAsProject()
        {
            Save();
            return true;
        }

        private static bool FrameManager_saveProject()
        {
            Save();
            return true;
        }
        static public void Save()
        {
            ioSetting.Save();
        }
        static public void AddNewInput(IOInputItem newItem)
        {
            ioSetting.listInputSettings.Add(newItem);
            ioSetting.dicInputSettings = ioSetting.listInputSettings.ToDictionary(p => p.strName);
        }
        static public void RemoveInput(int index)
        {
            ioSetting.listInputSettings.RemoveAt(index);
            ioSetting.dicInputSettings = ioSetting.listInputSettings.ToDictionary(p => p.strName);
        }
        static public void ModifyInput(int index, IOInputItem newItem)
        {
            ioSetting.listInputSettings[index].strName = newItem.strName;
            ioSetting.dicInputSettings = ioSetting.listInputSettings.ToDictionary(p => p.strName);
        }
        static public void InsertInput(int index, IOInputItem newItem)
        {
            ioSetting.listInputSettings.Insert(index, newItem);
            ioSetting.dicInputSettings = ioSetting.listInputSettings.ToDictionary(p => p.strName);
        }
        static public void AddNewOutput(IOOutputItem newItem)
        {
            ioSetting.listOutputSettings.Add(newItem);
            ioSetting.dicOutputSettings = ioSetting.listOutputSettings.ToDictionary(p => p.strName);
        }
        static public void RemoveOutput(int index)
        {
            ioSetting.listOutputSettings.RemoveAt(index);
            ioSetting.dicOutputSettings = ioSetting.listOutputSettings.ToDictionary(p => p.strName);
        }
        static public void ModifyOutput(int index, IOOutputItem newItem)
        {
            ioSetting.listOutputSettings[index].strName = newItem.strName;
            ioSetting.dicOutputSettings = ioSetting.listOutputSettings.ToDictionary(p => p.strName);
        }
        static public void InsertOutput(int index, IOOutputItem newItem)
        {
            ioSetting.listOutputSettings.Insert(index, newItem);
            ioSetting.dicOutputSettings = ioSetting.listOutputSettings.ToDictionary(p => p.strName);
        }
        static public IOInputItem Input(string strName)
        {
            if (ioSetting.dicInputSettings.Keys.Contains(strName))
            {
                return ioSetting.dicInputSettings[strName];
            }
            else
            {
                return null;
            }
        }
        static public IOOutputItem Output(string strName)
        {
            if (ioSetting.dicOutputSettings.Keys.Contains(strName))
            {
                return ioSetting.dicOutputSettings[strName];
            }
            else
            {
                return null;
            }
        }
        public static Form OpenSettingForm()
        {
            FormIOSetting frmSetting = new FormIOSetting();
            frmSetting.TopLevel = false;
            return frmSetting;
        }
        public static Form OpenDebugForm()
        {
            FormIODebug frmSetting = new FormIODebug();
            frmSetting.TopLevel = false;
            return frmSetting;
        }
    }
}
